#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <inttypes.h>

#include "esp_log.h"

#include "esp_bt_main.h"
#include "esp_gap_bt_api.h"
#include "esp_bt_device.h"
#include "esp_a2dp_api.h"
#include "esp_avrc_api.h"
#include "esp_gatt_common_api.h"
#include "esp_gatts_api.h"

#include "btCfg.h"
// 定义BLE服务UUID
#define GATTS_TAG "BT_DUAL_MODE"
#define GATTS_SERVICE_UUID_TEST    0x00FF
#define GATTS_CHAR_UUID_TEST_A     0xFF01
#define GATTS_CHAR_UUID_TEST_B     0xFF02
#define GATTS_NUM_HANDLE_TEST      4

#define PROFILE_A_APP_ID 0

// BLE设备名称
static const char *ble_device_name = "ESP32_Audio_Device";
// 经典蓝牙设备名称
static const char *bt_device_name = "ESP32_Audio_Sink";

#define BT_CFG_TAG  "bt controller init"

// 全局变量
static bool ble_connected = false;
static bool bt_connected = false;



static char *bda2str(uint8_t * bda, char *str, size_t size)
{
    if (bda == NULL || str == NULL || size < 18) {
        return NULL;
    }

    uint8_t *p = bda;
    sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
            p[0], p[1], p[2], p[3], p[4], p[5]);
    return str;
}

static void bt_controller_Init(esp_bt_mode_t mode)
{
    esp_err_t err;

    ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
    esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
    if ((err = esp_bt_controller_init(&bt_cfg)) != ESP_OK) {
        ESP_LOGE(BT_CFG_TAG, "%s initialize controller failed: %s", __func__, esp_err_to_name(err));
        return;
    }
    if ((err = esp_bt_controller_enable(mode)) != ESP_OK) {
        ESP_LOGE(BT_CFG_TAG, "%s enable controller failed: %s", __func__, esp_err_to_name(err));
        return;
    }
}

static void bt_bluedroid_cfg_with_default(void)
{
    esp_err_t err;

    esp_bluedroid_config_t bluedroid_cfg = BT_BLUEDROID_INIT_CONFIG_DEFAULT();
#if (CONFIG_EXAMPLE_A2DP_SINK_SSP_ENABLED == false)
    bluedroid_cfg.ssp_en = false;
#endif
    if ((err = esp_bluedroid_init_with_cfg(&bluedroid_cfg)) != ESP_OK) {
        ESP_LOGE(BT_CFG_TAG, "%s initialize bluedroid failed: %s", __func__, esp_err_to_name(err));
        return;
    }

    if ((err = esp_bluedroid_enable()) != ESP_OK) {
        ESP_LOGE(BT_CFG_TAG, "%s enable bluedroid failed: %s", __func__, esp_err_to_name(err));
        return;
    }
}

void bt_Init_BTDM(esp_gap_ble_cb_t gap_ble_cb, esp_gattc_cb_t gattc_cb)
{
    char bda_str[18] = {0};

    bt_controller_Init(ESP_BT_MODE_BTDM);
    bt_bluedroid_cfg_with_default();

    //ble
    esp_err_t ret = esp_ble_gap_register_callback(gap_ble_cb);
    if (ret){
        ESP_LOGE(BT_CFG_TAG, "%s gap register failed, error code = %x", __func__, ret);
        return;
    }

    // Register the callback function to the gattc module
    ret = esp_ble_gattc_register_callback(gattc_cb);
    if(ret){
        ESP_LOGE(BT_CFG_TAG, "%s gattc register failed, error code = %x", __func__, ret);
        return;
    }

    ret = esp_ble_gattc_app_register(PROFILE_A_APP_ID);
    if (ret){
        ESP_LOGE(BT_CFG_TAG, "%s gattc app register failed, error code = %x", __func__, ret);
    }
    esp_err_t local_mtu_ret = esp_ble_gatt_set_local_mtu(500);
    if (local_mtu_ret){
        ESP_LOGE(BT_CFG_TAG, "set local  MTU failed, error code = %x", local_mtu_ret);
    }

    //classic bt
#if (CONFIG_EXAMPLE_A2DP_SINK_SSP_ENABLED == true)
    /* set default parameters for Secure Simple Pairing */
    esp_bt_sp_param_t param_type = ESP_BT_SP_IOCAP_MODE;
    esp_bt_io_cap_t iocap = ESP_BT_IO_CAP_IO;
    esp_bt_gap_set_security_param(param_type, &iocap, sizeof(uint8_t));
#endif

    /* set default parameters for Legacy Pairing (use fixed pin code 1234) */
    esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_FIXED;
    esp_bt_pin_code_t pin_code;
    pin_code[0] = '1';
    pin_code[1] = '2';
    pin_code[2] = '3';
    pin_code[3] = '4';
    esp_bt_gap_set_pin(pin_type, 4, pin_code);

    ESP_LOGI(BT_CFG_TAG, "Own address:[%s]", bda2str((uint8_t *)esp_bt_dev_get_address(), bda_str, sizeof(bda_str)));
}

void bt_Init_ClassicBt(void)
{
    char bda_str[18] = {0};

    bt_controller_Init(ESP_BT_MODE_CLASSIC_BT);
    bt_bluedroid_cfg_with_default();
    //classic bt
#if (CONFIG_EXAMPLE_A2DP_SINK_SSP_ENABLED == true)
    /* set default parameters for Secure Simple Pairing */
    esp_bt_sp_param_t param_type = ESP_BT_SP_IOCAP_MODE;
    esp_bt_io_cap_t iocap = ESP_BT_IO_CAP_IO;
    esp_bt_gap_set_security_param(param_type, &iocap, sizeof(uint8_t));
#endif

    /* set default parameters for Legacy Pairing (use fixed pin code 1234) */
    esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_FIXED;
    esp_bt_pin_code_t pin_code;
    pin_code[0] = '1';
    pin_code[1] = '2';
    pin_code[2] = '3';
    pin_code[3] = '4';
    esp_bt_gap_set_pin(pin_type, 4, pin_code);

    ESP_LOGI(BT_CFG_TAG, "Own address:[%s]", bda2str((uint8_t *)esp_bt_dev_get_address(), bda_str, sizeof(bda_str)));
}

void bt_Init_Ble(esp_gap_ble_cb_t gap_ble_cb, esp_gattc_cb_t gattc_cb)
{
    char bda_str[18] = {0};

    bt_controller_Init(ESP_BT_MODE_BLE);
    bt_bluedroid_cfg_with_default();

    //ble
    esp_err_t ret = esp_ble_gap_register_callback(gap_ble_cb);
    if (ret){
        ESP_LOGE(BT_CFG_TAG, "%s gap register failed, error code = %x", __func__, ret);
        return;
    }

    // Register the callback function to the gattc module
    ret = esp_ble_gattc_register_callback(gattc_cb);
    if(ret){
        ESP_LOGE(BT_CFG_TAG, "%s gattc register failed, error code = %x", __func__, ret);
        return;
    }

    ret = esp_ble_gattc_app_register(PROFILE_A_APP_ID);
    if (ret){
        ESP_LOGE(BT_CFG_TAG, "%s gattc app register failed, error code = %x", __func__, ret);
    }
    esp_err_t local_mtu_ret = esp_ble_gatt_set_local_mtu(500);
    if (local_mtu_ret){
        ESP_LOGE(BT_CFG_TAG, "set local  MTU failed, error code = %x", local_mtu_ret);
    }

    ESP_LOGI(BT_CFG_TAG, "Own address:[%s]", bda2str((uint8_t *)esp_bt_dev_get_address(), bda_str, sizeof(bda_str)));
}